package com.wjc.http;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.Iterator;
import java.util.LinkedHashMap;
 
/**
 * <p>Title: HttpRequest</p> 
 * <p>Description: </p> 
 * <p>Copyright: Copyright (c) 2006</p> 
 * @author 
 * @version 1.0
 */
public class HttpRequest
{
	private String firstLine = "";
	private String headerBody = "";
	
    private String method       = null;
    private String requestURI   = null;
    private String requestURL   = null;
    private String queryString  = null;
    private String httpProtocol = null;
    
    private Map<String, String> headers = new LinkedHashMap<String, String>();
    
    private InputStream inputStream;
    
    private OutputStream outputStream;
    private boolean  showHost;
 
    /**
     * 
     * @return - Map
     * @author: 
     */
    public Map<String, String> getHeaders()
    {
        return headers;
    }
 
	public boolean isClosed() {
		String value = (String) this.headers.get("Connection");
		if( null != value && value.equalsIgnoreCase("close")) {
			return true;
		}
		return false;
	}
	
	public String getHost() {
		String value = (String) this.headers.get("Host");
		if( null != value ) {
			return value;
		}
		return "";
	}
    
    /**
     * 
     * @param headers - void
     * @author: 
     */
    public void setHeaders(Map<String, String> headers)
    {
        this.headers = headers;
    }
    
    /**
     * 
     * @param name
     * @param value - void
     * @author: 
     */
    public void setHeader(String name, String value)
    {
        this.headers.put(name, value);
    }
    
    /**
     * 
     * @param name
     * @return - String
     * @author: 
     */
    public String getHeader(String name)
    {
        return (String)(this.headers.get(name));
    }
 
    /**
     * 
     * @return - String
     * @author: 
     */
    public String getHttpProtocol()
    {
        return httpProtocol;
    }
 
    /**
     * 
     * @param httpProtocol - void
     * @author: 
     */
    public void setHttpProtocol(String httpProtocol)
    {
        this.httpProtocol = httpProtocol;
    }
 
    /**
     * 
     * @return - String
     * @author: 
     */
    public String getMethod()
    {
        return method;
    }
 
    /**
     * 
     * @param method - void
     * @author: 
     */
    public void setMethod(String method)
    {
        this.method = method;
    }
 
    /**
     * 
     * @return - String
     * @author: 
     */
    public String getQueryString()
    {
        return queryString;
    }
 
    /**
     * 
     * @param queryString - void
     * @author: 
     */
    public void setQueryString(String queryString)
    {
        this.queryString = queryString;
    }
 
    /**
     * 
     * @return - String
     * @author: 
     */
    public String getRequestURI()
    {
        return requestURI;
    }
 
    /**
     * 
     * @param requestURI - void
     * @author: 
     */
    public void setRequestURI(String requestURI)
    {
        this.requestURI = requestURI;
    }
 
    /**
     * 
     * @return - String
     * @author: 
     */
    public String getRequestURL()
    {
        return requestURL;
    }
 
    /**
     * 
     * @param requestURL - void
     * @author: 
     */
    public void setRequestURL(String requestURL)
    {
        this.requestURL = requestURL;
    }
    
    /**
     * 
     * @return - String
     * @author: 
     */
    public String getContentType()
    {
        return (String)this.headers.get("Content-Type");
    }
    
    /**
     * 
     * @return - int
     * @author: 
     */
    public int getContentLength()
    {
        String value = (String)this.headers.get("Content-Length");
        
        int contentLength = -1;
        
        if(value == null)
        {
            return contentLength;
        }
        
        try
        {
            contentLength = Integer.parseInt(value);
        }
        catch(NumberFormatException e){}
        
        return contentLength;
    }
    
    public String getRemoteHost()
    {
        String host = (String)this.headers.get("Host");
        
        if(host != null)
        {
            int k = host.indexOf(":");
            
            if(k > -1)
            {
                return host.substring(0, k);
            }
            
            return host;
        }
        
        return null;
    }
    
    public int getRemotePort()
    {
        String host = (String)this.headers.get("Host");
        
        int port = 80;
        
        if(host != null)
        {
            int k = host.indexOf(":");
            
            if(k > -1)
            {
                try
                {
                    port = Integer.parseInt(host.substring(k + 1));
                }
                catch(NumberFormatException e){}
                
                return port;
            }
            
            return port;
        }
        
        return port;
    }
    
    /**
     * 
     * @return - InputStream
     * @author: 
     */
    public InputStream getInputStream()
    {
        return inputStream;
    }
 
    /**
     * 
     * @param inputStream - void
     * @author: 
     */
    public void setInputStream(InputStream inputStream)
    {
        this.inputStream = inputStream;
    }
 
    /**
     * 
     * @return - OutputStream
     * @author: 
     */
    public OutputStream getOutputStream()
    {
        return outputStream;
    }
 
    /**
     * 
     * @param outputStream - void
     * @author: 
     */
    public void setOutputStream(OutputStream outputStream)
    {
        this.outputStream = outputStream;
    }
    
   
	public String toString() {
		return this.headerBody;
	}

	public  boolean isShowHost() {
		return showHost;
	}

	public  void setShowHost(boolean showHost) {
		this.showHost = showHost;
	}

	public String getFirstLine() {
		return firstLine;
	}

	public void setFirstLine(String firstLine) {
		this.firstLine = firstLine;
	}

	public String getHeaderBody() {
		return headerBody;
	}

	public void setHeaderBody(String headerBody) {
		this.headerBody = headerBody;
	}
    
  
}
